//
//  UILabel+ZJChangeLineSpaceAndWordSpace.m
//  LovingPartyBuilding
//
//  Created by ZJ on 2017/5/3.
//  Copyright © 2017年 刘应平. All rights reserved.
//

#import "UILabel+ZJChangeLineSpaceAndWordSpace.h"
#import "NSString+additions.h"
#import "UIView+Extension.h"
#define UILABEL_LINE_SPACE 6
#define HEIGHT [ [ UIScreen mainScreen ] bounds ].size.height
@implementation UILabel (ZJChangeLineSpaceAndWordSpace)

+ (void)changeLineSpaceForLabel:(UILabel *)label WithSpace:(float)space {
    CGFloat labelWidth = label.width;
    NSString *labelText = label.text;
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:labelText];
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    [paragraphStyle setLineSpacing:space];
    [attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [labelText length])];
    label.attributedText = attributedString;
//    [label sizeToFit];
    label.width = labelWidth;
}

+ (void)changeWordSpaceForLabel:(UILabel *)label WithSpace:(float)space {
    
    NSString *labelText = label.text;
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:labelText attributes:@{NSKernAttributeName:@(space)}];
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    [attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [labelText length])];
    label.attributedText = attributedString;
    [label sizeToFit];
    
}

+ (void)changeSpaceForLabel:(UILabel *)label withLineSpace:(float)lineSpace WordSpace:(float)wordSpace {
    
    NSString *labelText = label.text;
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:labelText attributes:@{NSKernAttributeName:@(wordSpace)}];
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    [paragraphStyle setLineSpacing:lineSpace];
    [attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [labelText length])];
    label.attributedText = attributedString;
    [label sizeToFit];
  
}

+ (CGFloat)zj_changeLineSpaceForLabel:(UILabel *)label WithSpace:(float)space{
    
    NSInteger row = [NSString getLineNum:label.text font:label.font labelWidth:label.width];
    
    label.height = (row + 1)* space + [NSString zj_sizeOfString:label.text font:label.font width:label.width].height;
    label.textAlignment = NSTextAlignmentRight;
    [UILabel changeLineSpaceForLabel:label WithSpace:space];
    return label.height;
}


-(void)setLabelSpace:(CGFloat)space{
    [self setLabelSpace:space MarkString:@"" markColor:self.textColor];
}
//给UILabel设置行间距和字间距
-(void)setLabelSpace:(CGFloat)space MarkString:(NSString *)markString markColor:(UIColor *)markColor{
    //行数
    NSInteger row = [NSString getLineNum:self.text font:self.font labelWidth:self.width];
    if (self.numberOfLines != 0) {
        row = row > self.numberOfLines ? self.numberOfLines : row;
    }
    NSMutableParagraphStyle *paraStyle = [[NSMutableParagraphStyle alloc] init];
    paraStyle.lineBreakMode = NSLineBreakByCharWrapping;
    paraStyle.alignment = NSTextAlignmentLeft;
    if (row == 1) {
        paraStyle.lineSpacing = 0.0;
    }else{
        paraStyle.lineSpacing = space;
    }
    paraStyle.hyphenationFactor = 1.0;
    paraStyle.firstLineHeadIndent = 0.0;
    paraStyle.paragraphSpacingBefore = 0.0;
    paraStyle.headIndent = 0;
    paraStyle.tailIndent = 0;
    //设置字间距 NSKernAttributeName:@1.5f
    NSDictionary *dic = @{NSFontAttributeName:self.font, NSParagraphStyleAttributeName:paraStyle, NSKernAttributeName:@1.5f
                          };
    
    NSMutableAttributedString *attributeStr = [[NSMutableAttributedString alloc] initWithString:self.text attributes:dic];
    NSRange range = NSMakeRange(0, markString.length);
    [attributeStr setAttributes:@{NSForegroundColorAttributeName:markColor} range:range];
    self.attributedText = attributeStr;
    
}

//计算UILabel的高度(带有行间距的情况)
- (CGFloat)getSpaceLabelHeightFromSpace:(CGFloat)Space{
    //行数
    NSInteger row = [NSString getLineNum:self.text font:self.font labelWidth:self.width];
    if (self.numberOfLines != 0) {
        row = row > self.numberOfLines ? self.numberOfLines : row;
    }
    
    NSMutableParagraphStyle *paraStyle = [[NSMutableParagraphStyle alloc] init];
    
    paraStyle.lineBreakMode = NSLineBreakByCharWrapping;
    paraStyle.alignment = NSTextAlignmentLeft;
    if (row == 1) {
        paraStyle.lineSpacing = 0.0;
    }else{
        paraStyle.lineSpacing = Space;
    }
    paraStyle.hyphenationFactor = 1.0;
    paraStyle.firstLineHeadIndent = 0.0;
    paraStyle.paragraphSpacingBefore = 0.0;
    paraStyle.headIndent = 0;
    paraStyle.tailIndent = 0;
    NSDictionary *dic = @{NSFontAttributeName:self.font, NSParagraphStyleAttributeName:paraStyle, NSKernAttributeName:@1.5f
                          };
   
    
    CGSize size = [@"张" boundingRectWithSize:CGSizeMake(self.width, HEIGHT) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil].size;
    
    return [NSString heightWithTextFont:self.font] * row + (row - 1) * paraStyle.lineSpacing;
}

@end
